---@param player ServerPlayer
local addFangkeSkill = function(player, skillName)
  local room = player.room
  local skill = Fk.skills[skillName]
  if (not skill) or skill.lordSkill or skill.switchSkillName
    or skill.frequency > 3 -- 锁定技=3 后面的都是特殊标签
    or player:hasSkill(skill, true) then
    return
  end

  local fangke_skills = player:getMark("js_fangke_skills")
  if fangke_skills == 0 then fangke_skills = {} end
  table.insert(fangke_skills, skillName)
  room:setPlayerMark(player, "js_fangke_skills", fangke_skills)

  --[[
  -- room:handleAddLoseSkills(player, skillName, nil, false)
  player:doNotify("AddSkill", json.encode{ player.id, skillName })

  if skill:isInstanceOf(TriggerSkill) or table.find(skill.related_skills,
    function(s) return s:isInstanceOf(TriggerSkill) end) then
    player:doNotify("AddSkill", json.encode{ player.id, skillName, true })
  end

  if skill.frequency ~= Skill.Compulsory then
  end
  --]]

  player:addFakeSkill(skill)
  player:prelightSkill(skill.name, true)
  skill:onAcquire(player)
end

---@param player ServerPlayer
---@param general General
local function addFangke(player, general, addSkill)
  local room = player.room
  room:addTableMarkIfNeed(player, "@&js_fangke", general.name)

  if not addSkill then return end
  for _, s in ipairs(general.skills) do
    addFangkeSkill(player, s.name)
  end
  for _, sname in ipairs(general.other_skills) do
    addFangkeSkill(player, sname)
  end
end

local yingmen = fk.CreateSkill {
  name = "yingmen",
  frequency = Skill.Compulsory,
}

local banned_fangke = {
  "starsp__xiahoudun",  -- 原因：无敌
  "shichangshi",   -- 原因：变将与休整
  "godjiaxu", "zhangfei","js__huangzhong", "liyixiejing", "olz__wangyun", "yanyan", "duanjiong", "wolongfengchu", "wuanguo",
  "os__wangling", -- 原因：没有可用技能
  "js__pangtong", "os__xia__liubei", -- 原因：发动技能逻辑缺陷
}

yingmen:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, _)
    return player:hasSkill(self)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local exclude_list = table.map(room.players, function(p)
      return p.general
    end)
    table.insertTable(exclude_list, banned_fangke)
    for _, p in ipairs(room.players) do
      local deputy = p.deputyGeneral
      if deputy and deputy ~= "" then
        table.insert(exclude_list, deputy)
      end
    end
    local n = 4
    local generals = table.random(room.general_pile, n)
    for _, g in ipairs(generals) do
      addFangke(player, Fk.generals[g], player:hasSkill("js__pingjian", true))
    end
  end,


})

yingmen:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, _)
    return target == player and player:hasSkill(self) and #player:getTableMark("@&js_fangke") < 4
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local exclude_list = table.map(room.players, function(p)
      return p.general
    end)
    table.insertTable(exclude_list, banned_fangke)
    for _, p in ipairs(room.players) do
      local deputy = p.deputyGeneral
      if deputy and deputy ~= "" then
        table.insert(exclude_list, deputy)
      end
    end

    local m = player:getMark("@&js_fangke")
    local n = 4 - #player:getTableMark("@&js_fangke")
    local generals = table.random(room.general_pile, n)
    for _, g in ipairs(generals) do
      addFangke(player, Fk.generals[g], player:hasSkill("js__pingjian", true))
    end
  end,
})

yingmen:addEffect(fk.SkillEffect, {
  can_refresh =function (self, event, target, player, data)
    return target == player and player:getMark("js_fangke_skills") ~= 0 and
    table.contains(player:getMark("js_fangke_skills"), data.skill.name)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, s in ipairs(data.skill.related_skills) do
      if s:isInstanceOf(StatusSkill) then
        room:addSkill(s)
      end
    end
  end,
})
return yingmen